package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.uqbike.constant.SysConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.DetentionArea;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.business.DetentionAreaService;
import com.tbit.uqbike.webmanager.dao.core.DetentionAreaDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yee_
 */
@Service
public class DetentionAreaServiceImpl extends ServiceImpl<DetentionAreaDao, DetentionArea> implements
        DetentionAreaService {

    @Autowired
    private DetentionAreaDao detentionAreaDao;
    @Autowired
    private CalibrateService calibrateService;

    @Override
    public Result<PageResult<DetentionArea>> listByAccountId(Integer accountId,
                                                             Integer page, Integer size, String name) {
        QueryWrapper<DetentionArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accountId", accountId);
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        List<DetentionArea> detentionAreaList = detentionAreaDao.getByAccountId(accountId);
        Map<Integer, Integer> detentionAreaMap = detentionAreaList.stream().collect(Collectors.toMap(DetentionArea::getDetentionId, DetentionArea::getTotal));
        IPage<DetentionArea> detentionAreaPage = detentionAreaDao.selectPage(Page.of(page, size), queryWrapper);
        for (DetentionArea record : detentionAreaPage.getRecords()) {
            record.setTotal(detentionAreaMap.getOrDefault(record.getDetentionId(), 0));
        }
        return Result.success(new PageResult<>(page, size, detentionAreaPage.getTotal(), (int) detentionAreaPage.getPages(), detentionAreaPage.getRecords()));
    }

    @Override
    public void add(DetentionArea detentionArea, Integer mapType) {
        //校准
        calibrateService.calDetentionArea(detentionArea, mapType, SysConstant.ORIGINAL);
        detentionAreaDao.insert(detentionArea);
    }

    @Override
    public void edit(DetentionArea detentionArea, Integer mapType) {
        DetentionArea detentionAreaDb = detentionAreaDao.selectById(detentionArea.getDetentionId());
        if (detentionAreaDb == null) {
            throw new BaseException("detentionArea.not.exist");
        }
        detentionArea.setAccountId(detentionArea.getAccountId());
        //校准
        calibrateService.calDetentionArea(detentionArea, mapType, SysConstant.ORIGINAL);
        detentionAreaDao.updateById(detentionArea);
    }

    @Override
    public Result<DetentionArea> getByDetentionId(Integer detentionId) {
        DetentionArea detentionArea = detentionAreaDao.selectById(detentionId);
        if (detentionArea == null) {
            throw new BaseException("detentionArea.not.exist");
        }
        return Result.success(detentionArea);
    }

    @Override
    public DetentionArea getByDetentionAreaName(Integer accountId, String detentionAreaName) {
        return detentionAreaDao.getByDetentionAreaName(accountId, detentionAreaName);
    }
}
